# Contributor: David Demelier <markand@malikania.fr>
# Maintainer: David Demelier <markand@malikania.fr>
pkgname=wpewebkit
pkgver=2.40.5
pkgrel=2
pkgdesc="WebKit port optimized for embedded devices"
url="https://wpewebkit.org"
arch="all"
license="other"
makedepends="
	at-spi2-core-dev
	bubblewrap
	cairo-dev
	clang
	cmake
	gi-docgen
	gobject-introspection-dev
	gperf
	gst-plugins-bad-dev
	gst-plugins-base-dev
	gtk-doc
	harfbuzz-dev
	lcms2-dev
	libavif-dev
	libepoxy-dev
	libgcrypt-dev
	libseccomp-dev
	libsoup3-dev
	libtasn1-dev
	libwpe-dev
	libwpebackend-fdo-dev
	libxkbcommon-dev
	libxslt-dev
	llvm
	ninja
	openjpeg-dev
	perl
	ruby-dev
	unifdef
	wayland-dev
	wayland-protocols
	woff2-dev
	xdg-dbus-proxy
	"
subpackages="$pkgname-dev $pkgname-doc"
source="https://wpewebkit.org/releases/wpewebkit-$pkgver.tar.xz
	armv6kz.patch
	initial-exec.patch
	patch-gettext.patch
	"
options="!check"

case "$CARCH" in
s390x|riscv64)
	;;
*)
	makedepends="$makedepends lld"
	;;
esac

build() {
	case "$CARCH" in
	s390x|armhf|armv7|x86|ppc64le)
		# llint/LowLevelInterpreter.cpp fails to build with fortify source here
		export CXXFLAGS="$CXXFLAGS -U_FORTIFY_SOURCE"
		;;
	esac

	case "$CARCH" in
	armv7)
		# clang fails to build armv7 due to some NEON related thing.
		# https://github.com/WebKit/WebKit/pull/1233 should fix it
		;;
	s390x|riscv64)
		# no lld on s390x, broken on riscv64
		export CC=clang
		export CXX=clang++
		;;
	*)
		# much lower build memory usage and better final size
		export CC=clang
		export CXX=clang++
		export LDFLAGS="$LDFLAGS -fuse-ld=lld"
		;;
	esac
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi

	export AR=llvm-ar
	export NM=llvm-nm
	export RANLIB=llvm-ranlib

	cmake -B build \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=Release \
		-DPORT=WPE \
		-DENABLE_MINIBROWSER=On \
		-DENABLE_JOURNALD_LOG=Off \
		-GNinja \
		$CMAKE_CROSSOPTS .
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
5874eedb6c605ee0663bf70f75c35b7badb3330bcd1bd36671ac3be1beee1bf27f0cfc1875fa5c1841ceb0030c0e7bd278fae7877921c29f365a5c377d8f134d  wpewebkit-2.40.5.tar.xz
8c89d4ac737a2bd6d970fec3ecb9d0b72d61ffb9a37d4b0b56bc0106914398a65319e940c593c0305fc40d6900aac2a8b4fc3bafc9a96062063d15abd1f5039d  armv6kz.patch
26f3df81758068a83bf770e1f8b48546e9ec2428d23cbc4e1c5cc7851c91ad1dfeeac89aea73568a5f498cd6c053aaab7e1af67e59a471ad2d0375c1c64cbd8a  initial-exec.patch
4316330f0c42fcfe800210bdbeabbb6bdcf532b71e2761550b8a753499d801fd0405cc961a516dfddfc28c3a6cf0c17b6db461ff51158238b8d874bf75b799f2  patch-gettext.patch
"
